<?php
/*
 * @Descripttion: 后台主业
 * @version: 
 * @Author: 北月
 * @Date: 2020-03-19 11:07:48
 * @LastEditors: Mr. Xu
 * @LastEditTime: 2021-01-11 15:11:06
 */
namespace app\admin\controller;

use think\Db;
use \think\Session;
use app\common\controller\CRedis as redis;
class Index extends Base{
	
	public $cache_time=0;
	/**
	* @Function: index 后台左侧菜单
	* @Author: 北月
	* @LastEditors: 蔡孟光
	* @Date: 2020-04-07 11:00:45
	* @LastEditTime: 2020-04-08 17:56:57
	* @param {type} 
	* @return: 
	*/
	public function index()
    {
		
		$menu_data=$this->cache_menu();
		if(empty(input('url'))){
			$load = '/admin/index/main.html';
			$type_key=0;
			$is_screen=1;
		}else{
			$load=input('url');
			$type_key=input('type_key');
			
			$is_screen=input('is_screen');
		}
		
		$this->assign('father_url',url($this->handle_url));//将入口渲染到页面
		$this->assign('type_key',$type_key);//渲染顶部选中
		$this->assign('is_screen',$is_screen);//渲染是否全屏
		$this->assign('domain_url',http_s().SIET_URL);//渲染站点url
		$this->assign('name',$menu_data['power_name']);
		$this->assign('company',[]);
		$this->assign('menus',array_values($menu_data['menus']));
		$this->assign('power_name',$menu_data['power_name']);
		$this->assign('load',$load);
		
        return $this->fetch();
    }

	/**
     * @Function: cache_menu  得到菜单数据
     * @Author: 北月
     * @LastEditors: 蔡孟光
     * @Date: 2020-04-07 11:00:55
     * @LastEditTime: 2020-04-08 17:56:57
     * @param {type} 
     * @return: 
     */
    public function cache_menu(){
		$power_name="";//角色 
		
		$common =[];
		$power = db('admin_cate')->where(['id'=>session("central_cate_id")])->find();//得到该用户角色
        $menu = db('admin_menu')->where(['is_display'=>1,'types'=>1])->where('id','in',$power['permissions'])->order('orders asc')->select();//得到该角色拥有权限
		// var_dump($menu);die;
		//添加url
        foreach ($menu as $key => $value) {
        	if(empty($value['parameter'])) {
        		$url = url($value['module'].'/'.$value['controller'].'/'.$value['function']);
        	} else {
                $url = url($value['module'].'/'.$value['controller'].'/'.$value['function'],$value['parameter']);
        	}
        	$menu[$key]['url'] = $url;
		}
        $menu_data['menus'] = $this->menulist($menu);//匹配所有权限
		$menu_data['power_name']=$power["name"];
		$menu_data=$menu_data;
			
        return $menu_data;
	}
	
	/**
      * @Function: menulist 对菜单数据进行排序
      * @Author: 北月
      * @LastEditors: 北月
      * @Date: 2020-04-07 11:01:00
      * @LastEditTime: 2020-04-01 17:56:57
      * @param {type} 
      * @return: 
      */
     protected function menulist($menu){
		$menus = array();
		//先找出顶级菜单
		foreach ($menu as $k => $val) {
			if($val['pid'] == 0) {
				$menus[$k] = $val;
			}
		}
		//通过顶级菜单找到下属的子菜单
		foreach ($menus as $k => $val) {
			foreach ($menu as $key => $value) {
				if($value['pid'] == $val['id']) {
					$menus[$k]['list'][] = $value;
				}
			}
		}
		//三级菜单
		foreach ($menus as $k => $val) {
			if(isset($val['list'])) {
				foreach ($val['list'] as $ks => $vals) {
					foreach ($menu as $key => $value) {
						if($value['pid'] == $vals['id']) {
							$menus[$k]['list'][$ks]['list'][] = $value;
						}
					}
				}
			}
		}//dump($menus);die;
		return $menus;
	}
	
	/**
  * @Function: main 控制台
  * @Author: 北月
  * @LastEditors: 北月
  * @Date: 2020-04-07 11:02:05
  * @LastEditTime: 2020-04-01 17:56:57
  * @param {type} 
  * @return: 
  */
	public function main()
    {
		// 访问数
			// 今日
			$data['visit_today'] 	= 11;
			$data['visit_week'] 	= 22;
			// 全部
			$data['visit_all'] 		= 33;
		// 项目数
			// 今日
			$data['project_today'] 	= 44;
			$data['project_week'] 	= 55;
			// 全部
			$data['project_all'] 	= 66;
			// var_dump($data);die;
		$time=date('Y-m-d').' - '.date('Y-m-d');
		$this->assign('time',$time);
		$this->assign('data',$data);
		
        return $this->fetch();
	}
	
	// 访问列表access_list
	public function access_list(){
		// if($this->request->isAjax()){
			$input = input();
			$where=[];
			if (isset($input['status_seache']) and !empty($input['status_seache'])) {
				$where['v.status'] = $input['status_seache'];//['like', '%' .  . '%']
			}
			if (isset($input['seach_time']) and !empty($input['seach_time'])) {
				$str = explode(' - ',$input['seach_time']);
                $star_time=$str[0]? $str[0] : '';
				$end_time=$str[1] ? $str[1] : '';
				
                $where['v.add_time']=['between',[strtotime($star_time),strtotime($end_time)+86400]];
			}
            $page       = ($input['page']-1)*$input['limit'];
            $limit      = $input['limit'];
			// var_dump($where);die;
			$data = Db::name('visit')->alias('v')
            ->join('project p','v.pro_id=p.id')
			->where($where)
			->field('v.*,p.name pro_name')
			->order('v.add_time desc')
			->limit($page,$limit)
			->select();
			
			$count = Db::name('visit')->alias('v')
            ->join('project p','v.pro_id=p.id')
			->where($where)
            ->count();
            return toSuccess('OK', $data,$count,ceil($count/$limit));
        // }
	}
	// 统计图1
	public function census_one(){
		$data['one']=[
			['name'=>'成功','value'=>Db::name('visit')->where('status',1)->count()],
			['name'=>'失败','value'=>Db::name('visit')->where('status',2)->count()],
			// ['name'=>'全部','value'=>Db::name('visit')->count()],
		];

		// $data['one_all']=[
		// 	['name'=>'全部记录','value'=>Db::name('visit')->count()],
		// ];
		
		$data['two'] = Db::name('project')->alias('p')
		->join('visit v','v.pro_id=p.id')
		->group('p.id')
		->field('count(v.pro_id) value,p.name name')
		->select();

		return toSuccess('OK', $data);
	}
	// 统计图2
	public function census_two(){
		$last_day=7;
		$time=24*60*60;
		$date_type='Y-m-d';
		$today=strtotime(date('Y-m-d',time()));
		for ($x=0; $x<$last_day; $x++) {
			$star_time=$today-$time*$x;
			$end_time=$star_time+86399;
			$time_list[$x]['name']=date($date_type,$star_time);//日期
			$time_list[$x]['stamp']=[$star_time,$end_time];
			$list['data_name'][]=$time_list[$x]['name'];
		}
		$project = Db::name('project')->field('id,name')->select();
		$back = [];
		foreach($project as $k=>$v){
			$back['name'][$k] = $v['name'];
			$back['data'][$k]['name'] = $v['name'];
			$back['data'][$k]['type'] = 'line';
			$where['pro_id'] = $v['id'];
			foreach($time_list as $tk=>$tv){
				// ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
				$back['time'][$tk] = $tv['name'];
				$star_time 	= $tv['stamp'][0];
				$end_time 	= $tv['stamp'][1];
				$back['data'][$k]['data'][$tk]=db("visit")
				->where($where)
				->where('add_time','between time',[$star_time,$end_time])
				->count();
			}
		}
		
		$data = $back;
		return toSuccess('OK', $data);
	}
	public function get_week($time = '', $format='Y-m-d'){
		$time = $time != '' ? $time : time();
		//获取当前周几
		$week = date('w', $time);
		$date = [];
		for ($i=0; $i<7; $i++){
		  $date[$i] = date($format ,strtotime( '-' . $i .' days', $time));
		}
		
		return $date;
	  }
}
?>